﻿

namespace Orid.Management.SOHO.Book.Client.Service
{


    using System;
    using System.ServiceModel;

   public class Client
   {

       private  ServiceBookClient bookClient;

       private  ServiceBookClient BookClient
       {
           get
           {
               if (bookClient ==null || bookClient.State == CommunicationState.Closed)
               {

                   //BasicHttpBinding binding = new BasicHttpBinding();
                   //binding.ReaderQuotas.MaxArrayLength = 65536000;
                   //binding.MaxBufferPoolSize = 65536000;
                   //binding.MaxReceivedMessageSize = 65536000; 

                   ////binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
                   //binding.Security.Mode = BasicHttpSecurityMode.None;
                    

                   //bookClient = new ServiceBookClient(binding, new EndpointAddress(wcfAddress));

                   WSHttpBinding binding = new WSHttpBinding
                                               {
                                                   ReaderQuotas = {MaxArrayLength = 65536000},
                                                   MaxBufferPoolSize = 65536000,
                                                   MaxReceivedMessageSize = 65536000
                                               };

                   //binding.ReceiveTimeout = TimeSpan.FromMinutes(10);
                   binding.Security.Mode = SecurityMode.Message;


                   bookClient = new ServiceBookClient(binding, new EndpointAddress(wcfAddress));
               }
               return bookClient;
           }
          
       }

       private readonly string wcfAddress;

       /// <summary>
       /// Initializes a new instance of the <see cref="Client"/> class.
       /// </summary>
       /// <param name="uri">Wcf的uri 完整地址</param>
       public Client(string  uri)
        {
            wcfAddress = uri; 
            
        }

       public bool AddSchedule(string operatorID,  DateTime dateFrom, DateTime dateTo)
       {
           bool bResult =false;
           try
           {
               if (BookClient.State != CommunicationState.Opened || BookClient.State != CommunicationState.Opening)
               {
                   BookClient.Open();
               }
               bResult = BookClient.AddSchedule(operatorID,  dateFrom, dateTo);
           }
           catch (Exception ex)
           {

               BookClient.Error(ex.Message);
           }
           finally
           {
               BookClient.Close();
           }

           return bResult;


       }

       public bool RemoveSchedule(string operatorID, DateTime dateFrom, DateTime dateTo)
       {
           bool bResult = false;
           try
           {
               if (BookClient.State != CommunicationState.Opened || BookClient.State != CommunicationState.Opening)
               {
                   BookClient.Open();
               }
               bResult = BookClient.RemoveSchedule(operatorID, dateFrom, dateTo);
           }
           catch (Exception ex)
           {

               BookClient.Error(ex.Message);
           }
           finally
           {
               BookClient.Close();
           }

           return bResult;
       }


       public DateTime GetCurrentTime()
       {
           DateTime workTime = DateTime.MinValue;
           try
           {
               if (BookClient.State != CommunicationState.Opened || BookClient.State != CommunicationState.Opening)
               {
                   BookClient.Open();
               }
               workTime = BookClient.GetCurrentTime();




           }
           catch (Exception ex)
           {

               BookClient.Error(ex.Message);
           }
           finally
           {
               BookClient.Close();
           }

           return workTime == DateTime.MinValue ? DateTime.Now : workTime;


       }


       public DateTime GetBookTime()
       {
           DateTime workTime = DateTime.MinValue;
           try
           {
               if (BookClient.State != CommunicationState.Opened || BookClient.State != CommunicationState.Opening)
               {
                   BookClient.Open();
               }
               workTime = BookClient.GetBookTime();




           }
           catch (Exception ex)
           {

               BookClient.Error(ex.Message);
           }
           finally
           {
               BookClient.Close();
           }

           return workTime == DateTime.MinValue ? DateTime.Now : workTime;


       }

       public Book.Service.BookSetting[] GetCurrentBookSetting()
       {
           Book.Service.BookSetting[] bookSettings = null;

           try
           {
               if (BookClient.State != CommunicationState.Opened || BookClient.State != CommunicationState.Opening)
               {
                   BookClient.Open();
               }
               bookSettings = BookClient.GetCurrentBookSetting();




           }
           catch (Exception ex)
           {

               BookClient.Error(ex.Message);
           }
           finally
           {
               BookClient.Close();
           }

           return bookSettings;
       }


       
       public Book.Service.BookRecord GetNextWorkTime(string operatorID, DateTime bookDate)
       {
           Book.Service.BookRecord  bookRecord  = null;

           try
           {
               if (BookClient.State != CommunicationState.Opened || BookClient.State != CommunicationState.Opening)
               {
                   BookClient.Open();
               }
               bookRecord = BookClient.GetNextWorkTime(operatorID, bookDate);

           }
           catch (Exception ex)
           {

               BookClient.Error(ex.Message);
           }
           finally
           {
               BookClient.Close();
           }

           return bookRecord;
       }


       public Book.Service.BookRecord[] GetBookList(string operatorID, DateTime bookDateFrom,DateTime bookDateTo)
       {
           Book.Service.BookRecord[] bookRecords = null;

           try
           {
               if (BookClient.State != CommunicationState.Opened || BookClient.State != CommunicationState.Opening)
               {
                   BookClient.Open();
               }
               bookRecords = BookClient.GetBookListThreeArgs(operatorID, bookDateFrom,bookDateTo);

           }
           catch (Exception ex)
           {

               BookClient.Error(ex.Message);
           }
           finally
           {
               BookClient.Close();
           }

           return bookRecords;
       }


       public Book.Service.BookRecord[] GetBookList(string operatorID, DateTime bookDate )
       {
           Book.Service.BookRecord[] bookRecords = null;

           try
           {
               if (BookClient.State != CommunicationState.Opened || BookClient.State != CommunicationState.Opening)
               {
                   BookClient.Open();
               }
               bookRecords = BookClient.GetBookListTwoArgs(operatorID, bookDate);

           }
           catch (Exception ex)
           {

               BookClient.Error(ex.Message);
           }
           finally
           {
               BookClient.Close();
           }

           return bookRecords;
       }


       public DateTime[] GetBookDays(string operatorID, DateTime dateFrom, DateTime dateTo)
       {
           DateTime[] bookDays = null;

           try
           {
               if (BookClient.State != CommunicationState.Opened || BookClient.State != CommunicationState.Opening)
               {
                   BookClient.Open();
               }
               bookDays = BookClient.GetBookDays(operatorID, dateFrom, dateTo);

           }
           catch (Exception ex)
           {

               BookClient.Error(ex.Message);
           }
           finally
           {
               BookClient.Close();
           }

           return bookDays;
       }

       public void Info(string  msg)
       {
           try
           {
               if (BookClient.State != CommunicationState.Opened || BookClient.State != CommunicationState.Opening)
               {
                   BookClient.Open();
               }
              
               BookClient.Info(msg);

           }
           catch (Exception ex)
           {

               BookClient.Error(ex.Message);
           }
           finally
           {
               BookClient.Close();
           }
       }

       public void Error(string msg)
       {
           try
           {
               if (BookClient.State != CommunicationState.Opened || BookClient.State != CommunicationState.Opening)
               {
                   BookClient.Open();
               }

               BookClient.Error(msg);

           }
           catch (Exception ex)
           {

               BookClient.Error(ex.Message);
           }
           finally
           {
               BookClient.Close();
           }
       }


       /// <summary>
       /// Adds the login action.
       /// </summary>
       /// <param name="operatorID">The operator ID.</param>
       /// <returns></returns>
       public bool AddLoginAction(string operatorID)
       {
           bool bResult = false;
           try
           {
               if (BookClient.State != CommunicationState.Opened || BookClient.State != CommunicationState.Opening)
               {
                   BookClient.Open();
               }
               bResult = BookClient.AddLoginAction(operatorID );
           }
           catch (Exception ex)
           {

               BookClient.Error(ex.Message);
           }
           finally
           {
               BookClient.Close();
           }

           return bResult;
       }


       /// <summary>
       /// Adds the logout action.
       /// </summary>
       /// <param name="operatorID">The operator ID.</param>
       /// <returns></returns>
       public bool AddLogoutAction(string operatorID)
       {
           bool bResult = false;
           try
           {
               if (BookClient.State != CommunicationState.Opened || BookClient.State != CommunicationState.Opening)
               {
                   BookClient.Open();
               }
               bResult = BookClient.AddLogoutAction(operatorID);
           }
           catch (Exception ex)
           {

               BookClient.Error(ex.Message);
           }
           finally
           {
               BookClient.Close();
           }

           return bResult;
       }

   }
}
